package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/device")
public class CheckDevnumController {
	
	@Autowired
	private BizService bizService;
	private static Logger logger = LoggerFactory.getLogger(GetClassInformation.class);
	
	/**
	 * 根据通讯号查询终端编号
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetDevnum", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String GetDevnum(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 通讯号
			String factoryNo = request.getParameter("factoryNo");
			
		    if(factoryNo != null && !factoryNo.isEmpty()){
		    	// 设备号
		    	Map<String, Object> map = bizService.getDevnumByPhone(factoryNo);
				if (map != null && map.size() > 0) {
					response.setErrorcode(0);
					response.setData(map);
				} else {
					response.setErrorcode(1);
					response.setMessage("该通讯号无对应的终端设备号");
				}
		    }else{
		    	response.setErrorcode(1);
				response.setMessage("通讯号不能为空");
		    }	

		} catch (Exception e) {
			logger.info("GetDevnum:"+ e.toString());
			response.setReturnMsg(0,"获取通讯号失败！");
		} finally {
			return response.toJSON();
		}

	}
}
